<template>
    <el-dialog
    title="添加角色"
    :visible.sync="showAddNew"
    width="40%"
    :before-close="handleClose">
    <el-form :model="roleInfo" :rules="rules" ref="roleInfo">
        <table>
            <tr>
                <td>角色名称</td>
                <td>
                    <el-form-item prop="roleName">
                        <el-input v-model="roleInfo.roleName"></el-input>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td>角色描述</td>
                <td>
                     <el-form-item prop="roleDesc">
                        <el-input v-model="roleInfo.roleDesc"></el-input>
                    </el-form-item>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                </td>
            </tr>
        </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addNewOne">确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    export default {
    	props: ['showAddNew'],
        data(){
            return {
                
                roleInfo: {},
                rules: {
                    roleName: [
                        {required: true,message:'请输入角色名称',trigger:'blur'},
                        { min: 3, max: 20, message: '角色名长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    roleDesc: [
                        {required: true,message:'请输入角色描述',trigger:'blur'},
                        { min: 3, max: 20, message: '角色描述长度在 3 到 10 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        computed: {
            
        },
        methods: {
            handleClose(){
                this.$parent.showAddNew= false;
            },
            addNewOne(){
               
                this.$refs['roleInfo'].validate((valid) => {
                    if (valid) {
                        this.$http.post(this.$apis.addNewRole,this.roleInfo)
                        .then((resp)=>{
                            this.$message({
                                message: resp.message,
                                type: resp.success?"success":"danger"
                            });
                            this.handleClose();
                            this.$store.dispatch("loadAllRole");
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    }
</script>
<style lang="scss" scoped>

</style>
